﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;

	public class PollChoiceService : IPollChoiceService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PollChoiceService"/> class.
		/// </summary>
		/// <param name="pollChoiceRepository">The poll choice repository.</param>
		public PollChoiceService(IPollChoiceRepository pollChoiceRepository)
		{
			this.PollChoiceRepository = pollChoiceRepository;
		}

		/// <summary>
		/// Gets or sets the poll choice repository.
		/// </summary>
		/// <value>The poll choice repository.</value>
		public IPollChoiceRepository PollChoiceRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the poll choice by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public PollChoice GetById(int id)
		{
			return this.PollChoiceRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the poll choices by specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public IList<PollChoice> GetAll(PollChoiceSpecification specification)
		{
			return this.PollChoiceRepository.GetAll(specification);
		}

		/// <summary>
		/// Creates the poll choice.
		/// </summary>
		/// <returns></returns>
		public PollChoice Create()
		{
			return new PollChoice
			{
				Votes = 0, 
				Voters = 0
			};
		}

		/// <summary>
		/// Inserts the specified poll choice.
		/// </summary>
		/// <param name="choice">The choice.</param>
		public void Insert(PollChoice choice)
		{
			choice.Validate();

			choice.Slug = choice.Title.Slug();

			this.PollChoiceRepository.Insert(choice);
		}

		/// <summary>
		/// Updates the specified poll choice.
		/// </summary>
		/// <param name="choice">The choice.</param>
		public void Update(PollChoice choice)
		{
			choice.Validate();

			choice.Slug = choice.Title.Slug();

			this.PollChoiceRepository.Update(choice);
		}

		/// <summary>
		/// Deletes the specified poll choice.
		/// </summary>
		/// <param name="choice">The choice.</param>
		public void Delete(PollChoice choice)
		{
			this.PollChoiceRepository.Delete(choice);
		}
	}
}